﻿#include <DCTft/DCMasterDevice.h>
#include "DCTft/DCFrame.h"

#include "bsp/bsp_flash.h"
#include "bsp/bsp_uart1.h"
//#include "app/app_comm.h"


#define DBG_TAG "dc"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

DCFrameRecv m_recv;
DCFrame m_frame;
uint8_t m_txbuff[1024];

static dc_msg_cb_t _callback = NULL;

const uint8_t cmd_pitch_up[] = {0xB1, 0x11, 0x00, 0x01, 0x00, 0x05, 0x10, 0x01, 0x02};
const uint8_t cmd_pitch_down[] = {0xB1, 0x11, 0x00, 0x01, 0x00, 0x07, 0x10, 0x01, 0x02};
const uint8_t cmd_screen_up[] = {0xB1, 0x11, 0x00, 0x01, 0x00, 0x09, 0x10, 0x01, 0x02};
const uint8_t cmd_screen_down[] = {0xB1, 0x11, 0x00, 0x01, 0x00, 0x0B, 0x10, 0x01, 0x02};
const uint8_t cmd_armrest_up[] = {0xB1, 0x11, 0x00, 0x01, 0x00, 0x0D, 0x10, 0x01, 0x02};
const uint8_t cmd_armrest_down[] = {0xB1, 0x11, 0x00, 0x01, 0x00, 0x0F, 0x10, 0x01, 0x02};
const uint8_t cmd_pedal_up[] = {0xB1, 0x11, 0x00, 0x01, 0x00, 0x11, 0x10, 0x01, 0x02};
const uint8_t cmd_pedal_down[] = {0xB1, 0x11, 0x00, 0x01, 0x00, 0x13, 0x10, 0x01, 0x02};

const uint8_t cmd_force_assit_open[] =       {0xB1, 0x11, 0x00, 0x02, 0x00, 0x09, 0x10, 0x01, 0x01};
const uint8_t cmd_force_assit_close[] =      {0xB1, 0x11, 0x00, 0x02, 0x00, 0x09, 0x10, 0x01, 0x00};


const uint8_t cmd_arm_unfold[] =         {0xB1, 0x11, 0x00, 0x03, 0x00, 0x1f, 0x10, 0x01, 0x02};
const uint8_t cmd_arm_storage[] =        {0xB1, 0x11, 0x00, 0x03, 0x00, 0x1d, 0x10, 0x01, 0x02};

const uint8_t cmd_arm_up[] =             {0xB1, 0x11, 0x00, 0x03, 0x00, 0x02, 0x10, 0x01, 0x02};
const uint8_t cmd_arm_down[] =           {0xB1, 0x11, 0x00, 0x03, 0x00, 0x07, 0x10, 0x01, 0x02};
const uint8_t cmd_arm_left[] =           {0xB1, 0x11, 0x00, 0x03, 0x00, 0x0f, 0x10, 0x01, 0x02};
const uint8_t cmd_arm_right[] =          {0xB1, 0x11, 0x00, 0x03, 0x00, 0x0d, 0x10, 0x01, 0x02};
const uint8_t cmd_arm_zoomin[] =         {0xB1, 0x11, 0x00, 0x03, 0x00, 0x0b, 0x10, 0x01, 0x02};
const uint8_t cmd_arm_zoonout[] =        {0xB1, 0x11, 0x00, 0x03, 0x00, 0x09, 0x10, 0x01, 0x02};

const uint8_t cmd_arm_downward[] =       {0xB1, 0x11, 0x00, 0x03, 0x00, 0x13, 0x10, 0x01, 0x02};
const uint8_t cmd_pedal_upward[] =       {0xB1, 0x11, 0x00, 0x03, 0x00, 0x11, 0x10, 0x01, 0x02};
const uint8_t cmd_pedal_leftdeflect[] =  {0xB1, 0x11, 0x00, 0x03, 0x00, 0x17, 0x10, 0x01, 0x02};
const uint8_t cmd_pedal_rightdeflect[] = {0xB1, 0x11, 0x00, 0x03, 0x00, 0x15, 0x10, 0x01, 0x02};
const uint8_t cmd_pedal_leftroll[] =     {0xB1, 0x11, 0x00, 0x03, 0x00, 0x1B, 0x10, 0x01, 0x02};
const uint8_t cmd_pedal_rightroll[] =    {0xB1, 0x11, 0x00, 0x03, 0x00, 0x19, 0x10, 0x01, 0x02};


const uint8_t cmd_locked_ctr_push[] =       {0xB1, 0x10, 0x00, 0x00, 0x00, 0x0A, 0x01};//按下
const uint8_t cmd_move_ctr_disable[] =      {0xB1, 0x04, 0x00, 0x00, 0x00, 0x05, 0x00};//禁止
const uint8_t cmd_adjust_ctr_disable[] =    {0xB1, 0x04, 0x00, 0x00, 0x00, 0x06, 0x00};//禁止
const uint8_t cmd_setting_ctr_disable[] =   {0xB1, 0x04, 0x00, 0x00, 0x00, 0x07, 0x00};//禁止

const uint8_t cmd_locked_ctr_bounce[] =     {0xB1, 0x10, 0x00, 0x00, 0x00, 0x0A, 0x00};//弹起
const uint8_t cmd_move_ctr_enable[] =       {0xB1, 0x04, 0x00, 0x00, 0x00, 0x05, 0x01};//使能
const uint8_t cmd_adjust_ctr_enable[] =     {0xB1, 0x04, 0x00, 0x00, 0x00, 0x06, 0x01};//使能
const uint8_t cmd_setting_ctr_enable[] =    {0xB1, 0x04, 0x00, 0x00, 0x00, 0x07, 0x01};//使能

const uint8_t cmd_pallet_locked[] =      {0xB1, 0x11, 0x00, 0x00, 0x00, 0x0A, 0x10, 0x01, 0x01};
const uint8_t cmd_pallet_unlocked[] =    {0xB1, 0x11, 0x00, 0x00, 0x00, 0x0A, 0x10, 0x01, 0x00};
const uint8_t cmd_switch_main[] =  { 0xB1,0x01, 0x00, 0x00};
const uint8_t cmd_switch_debug[] = { 0xB1,0x01, 0x00, 0x01};
const uint8_t cmd_switch_move[] =  { 0xB1,0x01, 0x00, 0x02};
const uint8_t cmd_switch_set[] =   { 0xB1,0x01, 0x00, 0x03};
const uint8_t cmd_switch_lock[] =  { 0xB1,0x01, 0x00, 0x04};


uint8_t cmd_surgical_already[] = {0xEE,0xB5,0xB1,0x01, 0x00, 0x04,0xFF,0xFC,0xFF,0xFF};
uint8_t cmd_surgical_back[] = {0xEE,0xB5,0xB1,0x01, 0x00, 0x00,0xFF,0xFC,0xFF,0xFF};

extern void pallet_locked_ctr(void);

bool on_recv_one_frame(DCFrame* frame)
{
    uint8_t* param = NULL;
    char* msg = NULL;
    char** p_msg = &msg;
    uint8_t** p_paramter = &param;
    uint16_t paramter_len = 3;

    switch (frame->paload[0])
    {
    case 0xb1:
    {
        if(memcmp(&frame->paload[0], cmd_switch_move, sizeof(cmd_switch_move)) == 0)
        {
            msg = "切换到移动";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }

        if(memcmp(&frame->paload[0], cmd_switch_debug, sizeof(cmd_switch_debug)) == 0)
        {
            msg = "切换到调试";
//            LOG_D("msg in******* %s", msg);
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }

        if(memcmp(&frame->paload[0], cmd_switch_main, sizeof(cmd_switch_main)) == 0)
        {
            msg = "切换到主界面";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_switch_set, sizeof(cmd_switch_set)) == 0)
        {
            msg = "切换到设置";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_pallet_locked, sizeof(cmd_pallet_locked)) == 0)
        {
            msg = "台车锁定";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_pallet_unlocked, sizeof(cmd_pallet_unlocked)) == 0)
        {
            msg = "台车解锁";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_force_assit_open, sizeof(cmd_force_assit_open)) == 0)
        {
            msg = "助力打开";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_force_assit_close, sizeof(cmd_force_assit_close)) == 0)
        {
            msg = "助力关闭";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_pitch_up, sizeof(cmd_pitch_up)) == 0)
        {
            msg = "台车上升";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_pitch_down, sizeof(cmd_pitch_down)) == 0)
        {
            msg = "台车下降";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }

        if(memcmp(&frame->paload[0], cmd_screen_up, sizeof(cmd_screen_up)) == 0)
        {
            msg = "左轮前进";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_screen_down, sizeof(cmd_screen_down)) == 0)
        {
            msg = "左轮后退";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }

        if(memcmp(&frame->paload[0], cmd_armrest_up, sizeof(cmd_armrest_up)) == 0)
        {
            msg = "右轮前进";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_armrest_down, sizeof(cmd_armrest_down)) == 0)
        {
            msg = "右轮后退";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }

        if(memcmp(&frame->paload[0], cmd_pedal_up, sizeof(cmd_pedal_up)) == 0)
        {
            msg = "同时前进";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }

        if(memcmp(&frame->paload[0], cmd_pedal_down, sizeof(cmd_pedal_down)) == 0)
        {
            msg = "同时后退";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_arm_unfold, sizeof(cmd_arm_unfold)) == 0)
        {
            msg = "调整臂一键展开";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_arm_storage, sizeof(cmd_arm_storage)) == 0)
        {
            msg = "调整臂一键收缩";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_arm_up, sizeof(cmd_arm_up)) == 0)
        {
            msg = "调整臂上升";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_arm_down, sizeof(cmd_arm_down)) == 0)
        {
            msg = "调整臂下降";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_arm_left, sizeof(cmd_arm_left)) == 0)
        {
            msg = "调整臂向左";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_arm_right, sizeof(cmd_arm_right)) == 0)
        {
            msg = "调整臂向右";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_arm_zoomin, sizeof(cmd_arm_zoomin)) == 0)
        {
            msg = "调整臂伸出";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_arm_zoonout, sizeof(cmd_arm_zoonout)) == 0)
        {
            msg = "调整臂收缩";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_arm_downward, sizeof(cmd_arm_downward)) == 0)
        {
            msg = "调整臂俯";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_pedal_upward, sizeof(cmd_pedal_upward)) == 0)
        {
            msg = "调整臂仰";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_pedal_leftdeflect, sizeof(cmd_pedal_leftdeflect)) == 0)
        {
            msg = "调整臂左偏摆";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_pedal_rightdeflect, sizeof(cmd_pedal_rightdeflect)) == 0)
        {
            msg = "调整臂右偏摆";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_pedal_leftroll, sizeof(cmd_pedal_leftroll)) == 0)
        {
            msg = "调整臂左翻滚";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        if(memcmp(&frame->paload[0], cmd_pedal_rightroll, sizeof(cmd_pedal_rightroll)) == 0)
        {
            msg = "调整臂右翻滚";
            if(_callback)
                _callback(&p_msg, &p_paramter, &paramter_len);
        }
        return false;
    }
    default:
    {

        return false;
    }
    }
}


void dctft_widget_locked_set(uint8_t locked)
{
    extern  uint8_t pallet_locked;
    uint16_t len;

    rt_enter_critical();
    if(pallet_locked != locked)
    {
        pallet_locked = locked;
        if(pallet_locked)
        {
            bsp_uart1_tx_transmit(cmd_surgical_already,sizeof(cmd_surgical_already));
            pallet_locked_ctr();
        }
        else{
            bsp_uart1_tx_transmit(cmd_surgical_back,sizeof(cmd_surgical_back));
        }
    }
    rt_exit_critical();
}

/* 线程入口 */
static void thread_entry(void* parameter)
{
    rt_uint32_t value;

    while (1)
    {
        uint8_t data = 0xaa;
//        rt_thread_mdelay(1000);
        if(bsp_uart1_rx_transmit(&data, RT_WAITING_FOREVER))
        {
//            bsp_uart3_tx_transmit(&data, 1);
//            LOG_D("%02x\r", data);
            if (m_recv.proc_one_byte(&m_frame, data))
            {
                if(on_recv_one_frame(&m_frame))
                {
//                    uint16_t len;
//                    m_frame.make_pack(m_txbuff, &len);
//                    bsp_uart1_tx_transmit(m_txbuff, len);
                }
            }
        }
    }
}

void dc_tft_init(void)
{
#define THREAD_STACK_SIZE   512
#define THREAD_PRIORITY     5
#define THREAD_TIMESLICE    20

    rt_thread_t tid = RT_NULL;
    /* 创建线程 1 */
    tid = rt_thread_create("dc_tft",
                            thread_entry, RT_NULL,
                            THREAD_STACK_SIZE,
                            THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid != RT_NULL)
        rt_thread_startup(tid);
}

void dc_tft_reg_proc(dc_msg_cb_t callback)
{
    _callback = callback;
}

